from flask import Flask,render_template,request,escape
from shoppingcar import shopping_car

app = Flask(__name__)
 
def log_request(req:"flask_request",res:str):
	with open("view.log","a") as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep="|")

@app.route("/")
def Hello():
	return "Welcome to sign in"

@app.route("/entry")
def hello_entry():
	return render_template("login.html",the_title="hello_entry web")

@app.route("/login")
def hello_login():
	return render_template("entry.html",the_title="Please choose your product",the_shopping_car= shopping_car)

@app.route("/search",methods=["POST"])
def Hello_search():
	account = request.form["account"]
	password = request.form["password"]
	results = account+password
	log_request(request,results)
	return render_template("results.html",the_results=results)

@app.route("/viewlog")
def hello_viewlog():
	with open("view.log","r") as log:
		contents = []
		for item in log:
			contents.append([])
			for i in item.split("|"):
				contents[-1].append(escape(i))
	# return str(contents)
	log_title = ["Form data","Remove_addr","User_agent","results"]
	return render_template("view.html",the_data=contents,the_log_title=log_title)

if __name__ == '__main__':
	app.run(debug=True)